'use strict';

const jwt = require('jsonwebtoken');
const { Controller } = require('egg');

class UserController extends Controller {
  // 登录
  async login() {
    const { ctx } = this;
    const { request, service } = ctx;
    const data = request.body;
    if (data.username === 'admin' && data.password === '123456') {
      const user = {
        id: -1,
        relname: '超级管理员',
        username: 'admin'
      }
      user.token = jwt.sign(user, this.config.keys);
      ctx.success(user);
    } else {
      const user = await service.user.loginByEmail(data);
      console.log('user',user)
      user.token = jwt.sign(user, this.config.keys);
      ctx.success(user);
    }

  }

  async editPassword() {
    const { request, service } = this.ctx;
    const res = await service.user.editPassword(request.body)
    this.ctx.success(res);
  }
}

module.exports = UserController;
